import { _decorator, Component, instantiate, Node, Vec3 } from 'cc';
import { BaseComponent } from '../../../Manager/UIManager/BaseComponent';
import { SingleEffect } from './SingleEffect';
import { GAME_PREFAB } from '../../../Data/GlobalData';
const { ccclass, property } = _decorator;

@ccclass('EffectLoad')
/**
 * 效果层
 */
export class EffectLoad extends BaseComponent {

    //效果数组
    private EffectArr: Node[] = [];

    //没有用
    private Nouse: Node[] = [];

    protected OnShow(): void {
        for (let se of this.EffectArr) {
            se.setParent(null);
            this.Nouse.push(se);
        }
        this.EffectArr = [];
    }

    /**
     * 创建效果
     */
    public CreateEffect(_pos: Vec3) {
        if (this.Nouse.length > 0) {
            let se = this.Nouse.pop();
            se.setParent(this.node);
            se.setWorldPosition(_pos);
            se.getComponent(SingleEffect).PlayAni();
            this.EffectArr.push(se);
        } else {
            let se: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.SingleEffect]);
            se.getComponent(SingleEffect).EffectLoad = this;
            se.setParent(this.node);
            se.setWorldPosition(_pos);
            se.getComponent(SingleEffect).PlayAni();
            this.EffectArr.push(se);
        }
    }

    /**
     * 移除效果
     */
    public RemoveEffect(_node: Node) {
        this.EffectArr.splice(this.EffectArr.indexOf(_node), 1);
        _node.setParent(null);
        this.Nouse.push(_node);
    }

}


